#include <QTime>
#include <QDebug>
#include <QObject>
#include <QTcpSocket>
#include <qtcpserver.h>
#include <qtcpsocket.h>
#include <QByteArray>
#include <QThread>
#include <time.h>

#include "WorkThreadManage/BackUdpThreadManage.h"
#include "Interface/PluginInterface/tecplugininterface.h"
#include "uniontecsystemdll_global.h"

#include "CommunicationMange/KukaRsiUdpServerDLL/kukarsiudpserverdll_global.h"

BackUdpThread::BackUdpThread(){
    ThreadStatus = ENUM_ThreadStatus::ThreadInit;
    PluginStatus = ENUM_PluginStatus::Ready;

    pkukatecPluginInstance = nullptr;

    mThreadLoopTime.LoopTime = 0;
    mThreadLoopTime.TimeLast = 0;
    mThreadLoopTime.TimeNow = 0;
}

BackUdpThread::~BackUdpThread(){
    ThreadStatus = ENUM_ThreadStatus::ThreadStop;
    QThread::quit();
    QThread::wait(10);
}

short BackUdpThread::KUKAPluginInstanceConnect(kukatecPluginInterface* tp){
    pkukatecPluginInstance = tp;
    return NOERROR;
}

double BackUdpThread::GetThreadLoopTime(){
    return mThreadLoopTime.LoopTime;
}

void BackUdpThread::run(){
    COM_ComKuKaRsiUdpServerInstanceInit();
    ThreadStatus = ENUM_ThreadStatus::ThreadRunning;
    for(;;){
        if(ThreadStatus == ENUM_ThreadStatus::ThreadRunning){
            mThreadLoopTime.TimeNow = QDateTime::currentDateTime().toMSecsSinceEpoch();
            mThreadLoopTime.LoopTime = mThreadLoopTime.TimeNow - mThreadLoopTime.TimeLast;
            if(mThreadLoopTime.LoopTime == 0) mThreadLoopTime.LoopTime = -1;
            mThreadLoopTime.TimeLast = mThreadLoopTime.TimeNow;

            if(PluginStatus == ENUM_PluginStatus::Ready){  }
            else{
                if(PluginStatus == ENUM_PluginStatus::Init){
                    if(pkukatecPluginInstance != NULL){
                        this->pkukatecPluginInstance->RegisterKUKAInitTask();
                        PluginStatus = ENUM_PluginStatus::InitDone;
                    }
                }
                else if(PluginStatus == ENUM_PluginStatus::InitDone){  }
                else if(PluginStatus == ENUM_PluginStatus::Running){
                    if(pkukatecPluginInstance != NULL){
                        if(COM_ComKuKaRsiUdpServerInstanceExist()){
                            COM_KuKaRsiUdpReadData();
                            this->pkukatecPluginInstance->RegisterKUKALoopTask();
                            COM_KuKaRsiUdpSendData();
                        }
                    }
                }
                else if(PluginStatus == ENUM_PluginStatus::Pause){  }
                else if(PluginStatus == ENUM_PluginStatus::Delete){
                    if(pkukatecPluginInstance != NULL){
                        this->pkukatecPluginInstance = nullptr;
                        PluginStatus = ENUM_PluginStatus::Ready;
                    }
                }
            }
        }
        else if(ThreadStatus == ENUM_ThreadStatus::ThreadStop){
            if(pkukatecPluginInstance != nullptr){
                pkukatecPluginInstance = nullptr;
            }
            if(COM_ComKuKaRsiUdpServerInstanceExist()){
                COM_ComKuKaRsiUdpServerInstanceRelease();
            }
            break;
        }
    }
}

short  BackUdpThread::PluginStatusTransitions(ENUM_PluginStatus Status){
    this->PluginStatus = Status;
    return NOERROR;
}
